package com.study.talk.app.other;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.study.talk.R;
import com.study.talk.databinding.ActivityImagePreviewBinding;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.view.ZoomImageView;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;


public class ImagePreviewActivity extends AppCompatActivity {

    private ArrayList<String> imageList;

    private Integer index = 0;

    private ActivityImagePreviewBinding activityImagePreviewBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityImagePreviewBinding = ActivityImagePreviewBinding.inflate(getLayoutInflater());
        setContentView(activityImagePreviewBinding.getRoot());

        imageList = GsonInstance.instance.fromJson(getIntent().getStringExtra("imageList"), new TypeToken<ArrayList<String>>() {
        }.getType());

        index = getIntent().getIntExtra("index", 0);
        initView();
    }

    private void initView() {

        activityImagePreviewBinding.imagePreviewBanner.setVisibility(View.INVISIBLE);
        activityImagePreviewBinding.imagePreviewBanner.setAdapter(new ImageAdapter(imageList), false)
                .setPageTransformer(null)
                .setIndicator(new CircleIndicator(ImagePreviewActivity.this))
                .addBannerLifecycleObserver(ImagePreviewActivity.this)
                .isAutoLoop(false)
                .setCurrentItem(index, false);
        activityImagePreviewBinding.imagePreviewBanner.setVisibility(View.VISIBLE);
    }

    class ImageAdapter extends BannerAdapter<String, ImageAdapter.BannerViewHolder> {

        public ImageAdapter(List<String> data) {
            //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
            super(data);
        }

        //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
        @Override
        public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_preview_item, parent, false);
            return new BannerViewHolder(view);
        }

        @Override
        public void onBindView(BannerViewHolder holder, String data, int position, int size) {
            Glide.with(ImagePreviewActivity.this).load(OkHttp.URL + data).into(holder.imageView);
        }

        static class BannerViewHolder extends RecyclerView.ViewHolder {
            ZoomImageView imageView;

            public BannerViewHolder(View view) {
                super(view);
                this.imageView = view.findViewById(R.id.image_preview_item_image);
            }
        }
    }
}